#include "StompedReality/Core/GameTime.h"
#include <iostream>
namespace StompedReality
{
GameTime::GameTime( )
{
  this->_frameRate = 1000.0 / 60.0;
#ifdef WIN32
  this->_freq = 0;
  this->_counterStart = 0;
#endif // ifdef WIN32
}

GameTime::~GameTime( )
{
}

void GameTime::updateTime( )
{
#ifdef WIN32
  while ( this->frameTime( ) < this->_frameRate )
  {
  }
#endif // ifdef WIN32
  this->_frameStart = this->totalTime( );
}

void GameTime::resetTime( )
{
#ifdef WIN32
  if ( !QueryPerformanceFrequency ( &li ) )
  {
  }
  this->_freq = double ( li.QuadPart ) / 1000.0;
  QueryPerformanceCounter ( &li );
  _counterStart = li.QuadPart;
#elif LINUX
  gettimeofday( &time, 0 );
  this->_counterStart = time.tv_usec;
#endif // ifdef WIN32
}

const double GameTime::lastUpdateTime( )
{
  return this->_frameStart;
}

const double GameTime::totalTime( )
{
#ifdef WIN32
  QueryPerformanceCounter ( &li );
  return double ( li.QuadPart - this->_counterStart ) / this->_freq;
#elif LINUX
  gettimeofday( &time, 0 );
  return ((double)time.tv_usec - this->_counterStart) / 1000;
#endif // ifdef WIN32
}

const double GameTime::frameTime( )
{
  return this->totalTime( ) - this->lastUpdateTime( );
}

}
